package persist

import (
	"context"
	"crawler/engine"
	"errors"
	"log"

	"gopkg.in/olivere/elastic.v5"
)

func ItemSaver(index string) (chan engine.Item, error) {
	client, e := elastic.NewClient(elastic.SetSniff(false))
	if e != nil {
		return nil, e
	}

	out := make(chan engine.Item)
	go func() {
		itemCount := 0
		for {
			item := <-out
			log.Printf("Item Serve Go item: #%d %v\n", itemCount, item)
			itemCount++

			err := save(client, index, &item)
			if err != nil {
				log.Printf("Item Saver: error saving item %v", item, err)
			}
		}
	}()
	return out, nil
}

// 持久化数据
func save(client *elastic.Client, index string, item *engine.Item) error {
	if item.Type == "" {
		return errors.New("must supply Type")
	}

	indexService := client.Index().
		Index(index).
		Type(item.Type).
		BodyJson(item)

	if item.Id != "" {
		indexService.Id(item.Id)
	}

	_, err := indexService.Do(context.Background())
	if err != nil {
		return nil
	}

	return nil
}
